<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 公共控制器
 * @author Calvin
 *
 */
class  MY_Controller extends CI_Controller{
	protected $uid = 0;
	protected $sid = 0;
	protected $uemail = "";
	protected $isLogin = TRUE;
	public function __construct(){
		parent::__construct();
		$this->load->language('base');
		$this->load->service('login_service');
		//if($this->auth->)
		if(!$this->login_service->is_login())
		{
			$this->not_login_action();
		}
		else
		{
			$this->load_variable();
		}
		/*$this->auth->access_valid("",
			array("files/index","trace/index","user/user_info","user/update_pwd","update_pwd_success","user/new_email","user/valid_phone","user/new_phone","user/save_new_phone_success","user/valid_phone_success","user/send_valid_email_success","user/send_new_email_success")
		);*/
	}
	private function load_variable()
	{
		$user_info = $this->login_service->get_user_info();
		$this->uid = $user_info['id'];
		$this->uemail = $user_info['email'];
		$this->sid = $this->session->session_id;
	}
	//判断有无登录
	/*private function check_login(){
		$user = $this->session->userdata('__USER__');
		if(!$user || !$user['id']){
			//当没有手动登录时判断有无自动登录
			if(isset($_COOKIE['auto'])){
				$auto = explode('|[%%]|', $_COOKIE['auto']);
				$where = array(
						'email' => $auto[0],
						'id'    => $auto[1],
						'uniqid'    => $auto[2]
				);
				$row = $this->db->get_where('user', $where)->row_array();
				if($row){
					//cookie合法
					$this->auth->set_session($row);
				} else {
					$this->not_login_action();
				}
			} elseif(isset($_COOKIE['tryout'])){
				//判断存在试用用户时
				$cookie_tryout = $_COOKIE['tryout'];
				$tryout = explode('|[%%]|', $cookie_tryout);
				$where = array(
						'email' => $tryout[0],
						'uniqid'    => $tryout[1]
				);
				$row = $this->db->get_where('user', $where)->row_array();
				if($row){
					//cookie合法
					$this->auth->set_session($row);
					//防止 cookie.tryout丢失，导致试用用户判断错误的问题
					$this->session->set_userdata('isTryout', 1);
				} else {
					$this->not_login_action();
				}			
			}else {
				$this->not_login_action();
			}
		} else {
			$isTryout = $this->session->userdata('isTryout');
			if($isTryout && !isset($_COOKIE['tryout'])){
				//试用session存在，而cookie不存在，这个用户可能已经激活了，需要更新user.session
				//当激活的链接在不同的浏览器打开时会出现, IE下试用后点注册时会出现，点试用后点注册未激活激活试用页面时会出现
				//IE下清cookie清不了session
				$user_result = $this->db->get_where('user', array('id'=>$user['id']))->row_array();
				if($user_result && $user_result['verify'] == 1){
					$this->auth->set_session($user_result);
					$this->session->unset_userdata('isTryout');
				} else {
					$this->not_login_action();
				}
			}
		}
		
		if($this->isLogin){
			if(!$user){
				$user = $this->session->userdata('__USER__');
			}
			$this->uid = $user['id'];
			$this->sid = $this->session->session_id;
			$this->uemail = $user['email'];
		}
	}*/
	
	//没有登录动作
	private function not_login_action(){

		if($this->input->is_ajax_request()){
			//$array['login_state'] = -1;
			//echo json_encode($array); die;
			return Common::ajax_return(1011);
		} else {
			redirect('login');
		}
		$this->isLogin = FALSE;
	}
	
	//头部用户数据
	protected function head_user_info(){
		$row =$this->login_service->get_user_info();
		$image = '';
		$nickname = $this->login_service->get_show_name($row);
		if($row['image']){
			$image = $row['image'];
		}
		
		//判断是否是试用用户
		$tryout = $this->login_service->is_tryout()?1:0;
		
		$data = array(
				'user_nickname' => $nickname,
				'user_image'    => $image,
				'tryout'        => $tryout
				);
		$this->template->assign($data);
		$this->template->assign ( "CONTROLLER_NAME", $this->router->class);
	}
	
	/*public function trans_commit(){
		if ($this->db->trans_status() === FALSE)
		{
			$this->db->trans_rollback();
		}else
		{
			$this->db->trans_commit();
		}
	}*/



	
}